import { Evaluate } from 'evaluate-model.js';
import { Config } from '../../utils/config.js';
var evaluate = new Evaluate();
var evalArr;

var imgs = [];
Page({
  data: {
    total_on_num:5,
    total_off_num:0,
    pro_on_num:5,
    pro_off_num: 0,
    shop_on_num: 5,
    shop_off_num: 0,
    wuliu_on_num: 5,
    wuliu_off_num: 0,
    select:true,
    files: [],
    product_img:"",
    inputVal:"",
    is_nickname:1,
    onLoading:false,
    btnArray:['提交','正在提交...'],
    btnText:"提交"
  },
  onLoad: function (options){
    imgs = [];
    this.data.shopid = parseInt(options.shopId);
    evalArr = wx.getStorageSync('evalArr');
    var id = options.id;
    for (var i = 0; i < evalArr.length;i++){
      if (id == evalArr[i].id){
        evalArr = evalArr[i];
        this.setData({
          product_img: evalArr.sku_image
        })
      }
    }

  },

  evalChangTap: function (e) {
    var status = e.currentTarget.dataset.status;
    var name = e.currentTarget.dataset.name;
    var id = e.currentTarget.id
    var on_num;

    if(name=="product"){
      if (status === 'on') {
        on_num = Number(id);
      } else {
        on_num = Number(id) + this.data.pro_on_num;
      }
      this.setData({
        pro_on_num: on_num,
        pro_off_num: 5 - on_num
      })
    }
    if (name == "wuliu") {
      if (status === 'on') {
        on_num = Number(id);
      } else {
        on_num = Number(id) + this.data.wuliu_on_num;
      }
      this.setData({
        wuliu_on_num: on_num,
        wuliu_off_num: 5 - on_num
      })
    }
    if (name == "shop") {
      if (status === 'on') {
        on_num = Number(id);
      } else {
        on_num = Number(id) + this.data.shop_on_num;
      }
      this.setData({
        shop_on_num: on_num,
        shop_off_num: 5 - on_num
      })
    }
    if (name == "total") {
      if (status === 'on') {
        on_num = Number(id);
      } else {
        on_num = Number(id) + this.data.total_on_num;
      }
      this.setData({
        total_on_num: on_num,
        total_off_num: 5 - on_num
      })
    }
  },
  getAnonymityToEvalTap:function(event){
    this.setData({
      select:!this.data.select
    })
    if (this.data.select){
      this.data.is_nickname=1;
    }else{
      this.data.is_nickname = 0;
    }
  },
  chooseImage: function (e) {
    var that = this;
    var url = Config.restUrl;
    url = url.slice(0, url.length - 4);
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
        wx.uploadFile({
          url: url+'/upload/images-upload?fileparam=image&thumb=1&width=100&height=100',
          filePath: res.tempFilePaths[0],
          name: 'image',
          formData: {},
          success: function (res) {
            var data = JSON.parse(res.data);
            var url = data.files[0].url;
            // files_url['url'] = files_url['url'].push(data.files[0].url)
            var thumbImg ;
            var img = {};
            img.url = url;
            img.thumbImg = data.files[0].thumbImg;
            imgs.push(img);
          }
        })
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  // 获取评价信息
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  getSubmitData:function(){
    var that = this ;
    if (that.data.onLoading) {
      return false;
    }
    var evalData = {
      "content": this.data.inputVal,
      "image": imgs,
      "total_stars": this.data.total_on_num,
      "des_stars": this.data.pro_on_num,
      "service_stars": this.data.shop_on_num,
      "delivery_stars": this.data.wuliu_on_num,
      "order_sku_id": evalArr.id,
      "order_no": evalArr.order_no,
      "goods_id": evalArr.goods_id,
      "is_nickname": this.data.is_nickname,
      "shop_id": this.data.shopid
    };
    that.data.onLoading = true;
    that.setData({
      btnText:that.data.btnArray[1]
    })
    evaluate.submitEvaluate(evalData,(data)=>{
      that.data.onLoading = false;
      that.setData({
        btnText: that.data.btnArray[0]
      })
      if(data.status==1){
        wx.showToast({
          title: '评价成功~',
          icon: 'none',
          duration: 2000
        })
        //获取页面栈
        var pages = getCurrentPages();
        if (pages.length > 1) {
          //上一个页面实例对象
          var prePage = pages[pages.length - 2];
          var pagetwo = pages[pages.length - 3];
          var pagethree = pages[pages.length - 4];
          //触发上个界面
          prePage.changeData && prePage.changeData();
          pagetwo.changeData && pagetwo.changeData();
          pagethree.changeData && pagethree.changeData()
        }
        //调到上一页面
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000) 
      }else {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
}) 

